package javapop.framework.parser;

import javapop.framework.ParseContext;
import javapop.framework.ParseInput;
import javapop.framework.ParseResult;
import javapop.framework.Parser;

public abstract class Filter<S> extends Parser<S> {
	private Parser<S> source;
	
	public Filter(Parser<S> source) {
		this.source = source;
	}
	
	public Parser<S> getSource() {
		return source;
	}
	
	@Override
	public String getDescription() {
		return source.getDescription();
	}
	
	@Override
	public boolean isMemoized() {
		return false;
	}
	
	public abstract ParseResult<S> filter(ParseContext<?> ctx, ParseResult<S> result);
	
	@SuppressWarnings("unchecked")
	@Override
	public final ParseResult<S> eval(ParseContext ctx, ParseInput input) {
		ParseResult<S> result = source.parse(ctx,input);
		if(result.isError()) {
			return (ParseResult<S>) result.asError();
		}
		
		return filter(ctx, result);
	}
}
